import { SchemaManage } from '@/components/SchemaComponent';
import { ManageCommonAPI } from '@/pages/type';
import { useEffect, useMemo, useRef, useState } from 'react';
import { connect } from 'umi';
import { warnManageService } from '../services';
import { getTableColumns } from '../mconfig';
import '../common/style.less';

const WarnManageView: React.FC<WarnManageAPI.IManageType> = ({ curRes }) => {
  /** 管理页ref */
  const schemaManageRef: any = useRef();

  /**  获取管理页数据 */
  const { data, loading, pagination, searchList, getWarnData, reloadRefresh } = warnManageService(curRes);

  const [tableList, setTableList] = useState([]);

  useEffect(() => {
    if (data?.rows) {
      setTableList(data?.rows);
    }
  }, [data]);

  /**  重置搜索表单方法 */
  const onReset = (values: any) => {
    getWarnData({ ...values });
  };

  /**  搜索列表 */
  const onSearch = (request: ManageCommonAPI.IGridRequestType) => {
    getWarnData({ ...request });
  };

  /** 分页change 方法 */
  const onPageChange = (current: any, pageSize: any, params: any) => {
    getWarnData({ pageSize, current, ...params });
  };

  /**新增操作按钮*/
  const actionList: any = useMemo(() => {
    return [];
  }, [curRes]);

  /** 获取columns方法 */
  const columns: Record<string, any>[] = useMemo(() => {
    return getTableColumns();
  }, [curRes]);

  return (
    <div
      style={{
        position: 'relative',
        overflow: 'hidden',
        height: '100%',
        width: '100%',
      }}
    >
      <SchemaManage
        ref={schemaManageRef}
        searchProps={{
          switchList: [],
          searchList,
          onSwitch: () => {},
          onReset,
          onSearch,
        }}
        actionList={actionList}
        tableProps={{
          columns,
          dataSource: tableList,
          loading,
          pagination: pagination as any,
          rowKey: 'id',
          onPageChange, //分页onchange
        }}
      />
    </div>
  );
};

export default connect(({ baseLogin }: Record<string, any>) => ({
  curRes: baseLogin?.permissions,
}))(WarnManageView);
